<?php
    include_once("../config.php");
    include_once(DAO_PATH . "/ComponentDAO.php");
    include_once(OBJECT_PATH . "/Component.php");

    $action = $_REQUEST['action'];

    if ($action == "getCompList")
    {
        $categoryId = $_REQUEST['cat'];

        $dao = new ComponentDAO();
        $components = $dao->getComponentList($categoryId);

        header ("Content-Type:text/xml");
        echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
        echo '<components>';
        
        // iterate through the ArrayObject list and print the details as xml
        $iterator = $components->getIterator();
        while ($iterator->valid())
        {
            $component = $iterator->current();
            echo "<component>";
            echo "<id>" . $component->getId() . "</id>";
            echo "<name>" . $component->getName() . "</name>";
            echo "<description>" . $component->getDescription() . "</description>";
            echo "<price>" . $component->getPrice() . "</price>";
            echo "<stock>" . $component->getStock() . "</stock>";
            echo "<category>" . $component->getCategory() . "</category>";
            echo "</component>";
            $iterator->next();
        }
        
        echo '</components>';
    }
    else if ($action == "getComp")
    {
        $componentId = $_REQUEST['comp'];

        $dao = new ComponentDAO();
        $component = $dao->getComponent($componentId);

        header ("Content-Type:text/xml");
        echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
        echo "<component>";
        echo "<name>" . $component->getName() . "</name>";
        echo "<description>" . $component->getDescription() . "</description>";
        echo "<price>" . $component->getPrice() . "</price>";
        echo "<stock>" . $component->getStock() . "</stock>";
        echo "</component>";
    }
?>
